package com.tbm.console.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.Controller2FTLConstant;
import com.tbm.console.proj.entity.ColumnEntity;
import com.tbm.console.proj.entity.SystemConfig;
import com.tbm.console.proj.service.ColumnService;
import com.tbm.console.proj.service.SystemConfigService;
import com.tbm.console.util.FileUpload;
import com.tbm.console.util.JurisdictionUtil;
import com.tbm.console.util.ObjectExcelView;
import com.tbm.console.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.omg.PortableServer.POA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@Slf4j
public class SystemConfigDevelopController extends BaseController {

    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private ColumnService columnService;
    /**
     * 常量维护
     * @return
     */
    @RequestMapping("/config-develop-list")
    public ModelAndView constantListManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "config/config-develop-list";
            Object attribute = this.getRequest().getSession().getAttribute(ConsoleConstant.BTNRIGHTS);
            List<PageData> buttonList = (List<PageData>) JSONArray.parse(attribute.toString());
            modelAndView.addObject("buttons", buttonList);
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * 列头配置
     * @return
     */
    @RequestMapping("/config-column-manage")
    public ModelAndView columnConfigManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "config/config-column-manage";
            Object attribute = this.getRequest().getSession().getAttribute(ConsoleConstant.BTNRIGHTS);
            List<PageData> buttonList = (List<PageData>) JSONArray.parse(attribute.toString());
            modelAndView.addObject("buttons", buttonList);
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/column-add/{id}")
    public ModelAndView columnAdd(@PathVariable("id") String id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ColumnEntity byId = columnService.findById(Integer.valueOf(id));
            modelAndView.addObject("entity", byId);
            List<String> allTables = columnService.findAllTables();
            modelAndView.addObject("list", allTables);
            viewName = "config/column-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/column-set/{id}")
    public ModelAndView columnSet(@PathVariable("id") String id) {
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ColumnEntity byId = columnService.findById(Integer.valueOf(id));
            String[] split = byId.getAllName().split(" ");
            List<String> splitFreezeList = Arrays.asList(byId.getFreezeName().split(" "));
            List<String> splitHiddenList = Arrays.asList(byId.getHiddenName().split(" "));
            int columnNumber = 2;
            int size = split.length % columnNumber == 0 ? split.length / columnNumber : (split.length / columnNumber + 1);
            StringBuffer columnsSB = new StringBuffer();
            int index = 0;
            for(int i=0;i<size;i++){
                columnsSB.append("<tr class=\"text-c\">");
                for(int j=0;j<columnNumber;j++){
                    index = i * 2 + j;
                    if(index<split.length){
                        columnsSB.append(" <th>"+split[index]+"</th>");
                        columnsSB.append("  <td><div class=\"formControls\">");

                        columnsSB.append(" <div class=\"check-box\"><input type=\"checkbox\" id=\""+split[index]+"\" name=\""+split[index]+"\" ");
                        if(splitFreezeList.contains(split[index])){
                            columnsSB.append("checked=\"checked\"");
                        }
                        columnsSB.append("></div>");
                        columnsSB.append("</div></td>");
                        columnsSB.append("<td><div class=\"formControls\">");
                        columnsSB.append(" <div class=\"check-box\"><input type=\"checkbox\" id=\""+split[index]+"_display"+"\" name=\""+split[index]+"_display"+"\"");
                        if(splitHiddenList.contains(split[index])){
                            columnsSB.append("checked=\"checked\"");
                        }
                        columnsSB.append("></div>");
                        columnsSB.append(" </div></td>");
                    }else if(index==split.length){
                        columnsSB.append(" <th> </th><td><div class=\"formControls\"></div></td><td><div class=\"formControls\"></div></td>");
                        columnsSB.append(" ");
                    }
                }
                columnsSB.append("</tr>");
            }
            modelAndView.addObject("entity", byId);
            modelAndView.addObject("code", columnsSB.toString());
            viewName = "config/column-set";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
}
